#pragma once
#ifndef xXMLParser_h__
#define xXMLParser_h__

#include "./skyTreeNode.h"
namespace Sky
{
	namespace NXMLParser
	{
		class xIDMap : public Sky::Memory
		{
		public:
			u32   id;
			Char* str;
		};
		//
		class xAttribute: public Sky::Memory
		{
		public:
			xIDMap* att;
			Char*  val;
			xAttribute* pNext;
			xAttribute():att(vNull),val(vNull),pNext(vNull){};
		};
		//
		class xElement : public Sky::Memory
		{
		public:
			xIDMap* ele;
			Char*	val;
			xAttribute* pAttr;
			xAttribute* pLast;
			xElement():ele(vNull),val(vNull),pAttr(vNull),pLast(vNull){};
			Void addAttribute(xIDMap* attr);
			Void setAttributeValue(Char* val);
			Void delAttribute();
		};
		//
		class xDomTreeNode : public TreeNode
		{
			DECL_RTTI1(xDomTreeNode,TreeNode);
		protected:
			xElement element;
		public:
			virtual Void setValue(Char* val,i32 len){}
			virtual Void setName(Char* name,i32 len){};
			virtual Void addAttributeName(Char* name,i32 len){};
			virtual Void addAttributeValue(Char* val,i32 len){};
		};
		class xDomTree
		{
			xElement mRoot;
		};
		//
		class xDomBuilder
		{
		public:
			virtual Void BeginDocument(){};
			virtual Bool BeginElement(){return vTrue;}
			virtual Bool ElementName(Char* ele,i32 len){return vTrue;};
			virtual Bool AttributeName(Char* attr,i32 len){return vTrue;};
			virtual Void AttributeValue(Char* val,i32 len){};
			virtual Void ElementValue(Char* val,i32 len){};
			virtual Void EndElement(){};
			virtual Void EndDocument(){};
		};
		
		class xXMLTinyParser
		{
		private:
			typedef enum _xState_
			{
				STATE_XML_XML,
				STATE_XML_ELEMENT_BEGIN,
				STATE_XML_ELEMENT_NAME,
				STATE_XML_ELEMENT_VALUE,
				STATE_XML_ATTRIBUTE_NAME,
				STATE_XML_ATTRIBUTE_VALUE,
				STATE_XML_ELEMENT_FINISH,
				STATE_XML_XML_FINISH

			}xState;
		private:
			i32   mLen;
			xState mState;
			Bool findElementName(Char** pBegin,Char** pEnd,i32* len,i32 left);
			Bool findElementValue(Char** pBegin,Char** pEnd,i32* len,i32 left);
			Bool findAttributeName(Char** pBegin,Char** pEnd,i32* len,i32 left);
			Bool findAttributeValue(Char** pBegin,Char** pEnd,i32* len,i32 left);
			Bool findElementFinish(Char** pBegin,Char** pEnd,i32* len,i32 left);
		public:
			Void Begin();
			Bool Parse(xDomBuilder* domMaker, Char* pData, i32 nSize);
			Void End();
		};
	}
}
#endif // xXMLParser_h__
